﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITAP.Core.Modelling.Primitives;
using ITAP.ShprossesBuilder.Calculations;
using ITAP.Core.Modelling;
using ITAP.Core.Calculations;

namespace ITAP.ShprossesBuilder.Modelling.Components.Actions
{
    public class ActionAlignSelectedShprossesAngles : IComponentAction
    {
        private ModelControl modelControl;
        private Angle angle;
        public ActionAlignSelectedShprossesAngles(ModelControl modelControl, Angle angle)
        {
            this.modelControl = modelControl;
            this.angle = angle;

        }
        #region IComponentAction Members

        public bool Action(IComponent component)
        {
            if(!(component is Shpross))
                return false;

            //List<Shpross> shprossesToAlign = new List<Shpross>();
            //foreach (IComponent selectedComponent in modelControl.ModelContainer.SelectedComponentsStack)
            //{
            //    if (selectedComponent is Shpross && selectedComponent != component)
            //        shprossesToAlign.Add(selectedComponent as Shpross);
            //}

            Shpross shpross = component as Shpross;


            List<IComponent> connectedComponents = new List<IComponent>(component.ConnectedComponents);

            foreach (IComponent connectedComponent in component.ConnectedComponents) //in connectedComponents) 
            {
                if (connectedComponent is StickConnector && connectedComponent != shpross.StartConnectedComponent && connectedComponent != shpross.EndConnectedComponent)
                {
                    Shpross connectedShpross = (Shpross)((connectedComponent as StickConnector).EndConnectedComponent);
                    if (connectedShpross.Selected)
                    {
                        this.FixAngle(shpross, connectedComponent as StickConnector);
                    }
                }
            }
            return true;
        }

        #endregion

        private void FixAngle(Shpross shpross, StickConnector stickConnector)
        {
            IWidePrimitive shprossPrimitive = (IWidePrimitive)shpross.Primitives[0];
            ModelVector shprossTangent = null;
            if (stickConnector.Sticked == StickConnectionType.ToUpBorder)
            {
                double offset = ((IEndedPrimitive)shprossPrimitive.UpBorder).GetPathLengthFromProjectionToStart(stickConnector.StartPoint);     
                shprossTangent = ((IEndedPrimitive)shprossPrimitive.UpBorder).GetTangent(offset / shprossPrimitive.UpBorder.Length);
            }
            else if (stickConnector.Sticked == StickConnectionType.ToDownBorder)
            {
                double offset = ((IEndedPrimitive)shprossPrimitive.DownBorder).GetPathLengthFromProjectionToStart(stickConnector.StartPoint);     
                shprossTangent = ((IEndedPrimitive)shprossPrimitive.DownBorder).GetTangent(offset / shprossPrimitive.UpBorder.Length);
                shprossTangent = shprossTangent.Rotate(new Angle(Math.PI));
            }
            else
                throw new Exception();
            Angle currentAngle = ModelCalculations.GetAngleBetweenVectors(shprossTangent, stickConnector.Primitives[0].GetTangent(0), false);
            Angle angleToRotate = angle - currentAngle;
            //stickConnector.Rotate(angleToRotate.Value);
            IEndedComponent endedComponent = stickConnector.EndConnectedComponent as IEndedComponent;
            endedComponent.Rotate(angleToRotate.Value, stickConnector.StartPoint, endedComponent.Parent.BoundingBox);
        }


    }
}
